package com.team7.home.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.common.entity.Article;
import com.team7.common.vo.ArticleVo;
import com.team7.common.vo.BottomArticleVo;
import com.team7.common.vo.CommonResult;
import com.team7.home.dao.ArticleDao;
import com.team7.home.service.ArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @program: team7-object-master
 * @description: 服务类
 * @author: 王金涛
 * @create: 2021-10-19 14:44
 **/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, Article> implements ArticleService {

    @Resource
    private ArticleDao articleDao;


    /**
     * 查询底部文章方法
     * @param navId
     * @return
     */
    @Override
    public CommonResult getArticleByNavId(String navId) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("nav_id",navId);
        Article article = articleDao.selectOne(wrapper);
        if (article!=null){
            return new CommonResult(2000,"查询成功",article);
        }else {
            return new CommonResult(5000,"查询失败",null);
        }
    }

    /**
     * 更改文章方法
     * @param articleVo
     * @return
     */
    @Override
    public CommonResult updateArticleByNavId(ArticleVo articleVo) {
        Article article = new Article();
        if (StringUtils.isNotEmpty(articleVo.getArtDesc())){
            article.setArtDesc(articleVo.getArtDesc());
        }
        if (StringUtils.isNotEmpty(articleVo.getArtPic())){
            article.setArtPic(articleVo.getArtPic());
        }
        if (StringUtils.isNotEmpty(articleVo.getArtTitle())){
            article.setArtTitle(articleVo.getArtTitle());
        }
        article.setGmtModified(LocalDateTime.now());
        article.setId(articleVo.getId());
        int i = articleDao.updateById(article);
        if (i>0){
            return new CommonResult(2000,"更新成功",null);
        }else {
            return new CommonResult(5000,"更新失败",null);
        }

    }

    @Override
    public int addArticle(BottomArticleVo bottomArticleVo) {
        Article article = new Article();
        article.setGmtModified(LocalDateTime.now());
        article.setArtTitle(bottomArticleVo.getName());
        article.setNavId(bottomArticleVo.getId());
        article.setGmtModified(LocalDateTime.now());
        article.setIsDeleted(0);
        int insert = articleDao.insert(article);
        return insert;
    }

    @Override
    public CommonResult getAllQianArticel(String id) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("nav_id",id);
        Article article = articleDao.selectOne(wrapper);

        return new CommonResult(2000,"成功",article);
    }
}
